package professor.aula10.com.fafica.telas;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import br.fafica.pi1.rightcode.Exception.CodigoInvalidoException;
import br.fafica.pi1.rightcode.Exception.NomeInvalidoException;
import br.fafica.pi1.rightcode.Fachada.Fachada;
import br.fafica.pi1.rightcode.Usuario.Usuario;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class telaExemplo2 {

	private JFrame frame;
	private JTextField textCodigo;
	private JTextField textNome;
	private Fachada  fachada;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					telaExemplo2 window = new telaExemplo2();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public telaExemplo2() {
		initialize();
		fachada.getInstancia();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 684, 303);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JButton btnCaralho = new JButton("Cadastrar");
		btnCaralho.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cadastrar();
			}
		});
		btnCaralho.setBounds(37, 150, 89, 23);
		frame.getContentPane().add(btnCaralho);
		
		JLabel lblNome = new JLabel("Nome:");
		lblNome.setBounds(78, 78, 46, 14);
		frame.getContentPane().add(lblNome);
		
		textCodigo = new JTextField();
		textCodigo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		textCodigo.setBounds(139, 49, 242, 20);
		frame.getContentPane().add(textCodigo);
		textCodigo.setColumns(10);
		
		JLabel lblCpf = new JLabel("Codigo:");
		lblCpf.setBounds(78, 52, 46, 14);
		frame.getContentPane().add(lblCpf);
		
		textNome = new JTextField();
		textNome.setText("");
		textNome.setBounds(139, 75, 242, 20);
		frame.getContentPane().add(textNome);
		textNome.setColumns(10);
		
		JButton btnRemover = new JButton("Remover");
		btnRemover.setBounds(289, 150, 89, 23);
		frame.getContentPane().add(btnRemover);
		
		JButton btnAlterar = new JButton("Alterar");
		btnAlterar.setBounds(415, 150, 89, 23);
		frame.getContentPane().add(btnAlterar);
		
		JButton btnListar = new JButton("Listar");
		btnListar.setBounds(541, 150, 89, 23);
		frame.getContentPane().add(btnListar);
		
		JButton btnPesquisar = new JButton("Pesquisar");
		btnPesquisar.setBounds(163, 150, 89, 23);
		frame.getContentPane().add(btnPesquisar);
	}
	
	private void cadastrar(){
		int codigo = Integer.parseInt(textCodigo.getText());
		String nome = textNome.getText();
		Usuario usuario = new Usuario(codigo,nome);
		try {
			fachada.AdicionarUsuario(usuario);
			JOptionPane.showMessageDialog(frame, usuario.toString());
		} catch (SQLException | CodigoInvalidoException | NomeInvalidoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		liparTela();
	}
	
	private void remover(){
		
	}
	
	private void alterar(){
		
	}
	
	private void listar(){
		
	}
	
	private void liparTela(){
		textCodigo.setText("");
		textNome.setText("");
	}
}
